

<?php

class ApiController extends Zend_Controller_Action {

    public static $METHOD = "method";
    public static $ID = "id";
    public static $COUNT = "count";
    public static $PAGE = "page";
    //site
    public static $GET_ALL_SITE = "getAllSite";
    //type
    public static $GET_ALL_TYPE = "getAllType";
    //place
    public static $GET_PLACE_BY_SITE_ID = "getPlaceBySiteId";
    public static $GET_PLACE_BY_TYPE_ID = "getPlaceByTypeId";
    public static $GET_PLACE_BY_BLOCK_ID = "getPlaceByBlockId";
    public static $GET_PLACE_DETAIL_BY_ID = "getPlaceDetailById";
    //menu 
    public static $GET_MENU_BY_ID = "getMenuById";
    //map 
    public static $GET_MAP_BY_ID = "getMapById";
    //review
    public static $GET_REVIEW_BY_ID = "getReviewById";
    //search
    public static $TYPE = "type";
    public static $TYPE_PLACE = "placeName"; //Search by name
    public static $TYPE_STREET = "streetName";
    public static $TYPE_FOOD = "typeFood";
    public static $TYPE_LOCATION = "aroundMe"; //Search by location
    public static $SEARCH_PLACE_NAME = "name"; //params
    public static $LON = "lon";
    public static $LAT = "lat";
    //common
    public static $APK = "apiKey";
    public static $SIG = "sig";
    //block
    public static $GET_BLOCK = "getBlock";

    public function indexAction() {
        
    }
    
    public function contentAction(){
        $this->_helper->viewRenderer->setNoRender(true);
        $url = $this->_request->getParam("url");        
        $html = $this->curl($url);
        echo $html;
    }
    
    public function curl($api, $method = null, $args = array()) {
        try {
            $args['method'] = $method;
            $response = self::sendRequest($api, $args);
        } catch (Exception $e) {
            return null;
        }
        return $response;
    }

    private function sendRequest($api, $args) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $api);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            throw new Exception(curl_error($ch));
        }

        curl_close($ch);
        return $response;
    }

    public function checkPermission($params) {
        if (!isset($params[self::$APK]) || !isset($params[self::$SIG])) {
            echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_API_OR_SIG);
            die;
        }

        if (!isset($params[self::$METHOD])) {
            echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$INVAILD_METHOD);
            die;
        }

        $apikey = $params[self::$APK];
        $model = new Model_App();
        $secretKey = $model->getSECRECTByAPIKEY($apikey);

        if (empty($secretKey)) {
            echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NOT_FOUND_APIKEY);
            die;
        }

        $requestParams = "";
        $sigRequest = $params[self::$SIG];
        foreach ($params as $key => $value) {
            if ($key != 'controller' && $key != 'action' && $key != 'module' && $key != self::$SIG) {
                $requestParams.= $key . '=' . $value;
            }
        }
        $md5RequestParam = md5($requestParams . $secretKey['secretKey']);

        if ($sigRequest != $md5RequestParam) {
            echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$INVAILD_PARAMS);
            echo '<br> correct Sig = ' . $md5RequestParam;
            die;
        }
    }

    public function searchAction() {
        $this->checkPermission($this->_request->getParams());
        $this->_helper->viewRenderer->setNoRender(true);
        $type = $this->_request->getParam(self::$TYPE);

        $count = $this->_request->getParam(self::$COUNT);
        $page = $this->_request->getParam(self::$PAGE);
        if ($count == null || $page == null) {
            echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$INVAILD_PAGE_OR_COUNT));
            die;
        }

        if ($type == null) {
            echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$INVAILD_TYPE_ON_SEARCH));
            die;
        }

        if ($type != null) {
            
            if($type == self::$TYPE_STREET){
                $streetName = $this->_request->getParam(self::$SEARCH_PLACE_NAME);
                $model = new Model_Place();
                $rs = $model->api_searchPlaceByStreetName($streetName, $page, $count);

                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
                
            }
            if($type == self::$TYPE_FOOD){
                $typeName = $this->_request->getParam(self::$SEARCH_PLACE_NAME);
                $model = new Model_Place();
                $rs = $model->api_searchPlaceByFoodName($typeName, $page, $count);

                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
                
                
            }
            if ($type == self::$TYPE_PLACE) {
                $placeName = $this->_request->getParam(self::$SEARCH_PLACE_NAME);

                $model = new Model_Place();
                $rs = $model->api_searchPlaceByPlaceName($placeName, $page, $count);

                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
            }
            if ($type == self::$TYPE_LOCATION) {
                $lon = $this->_request->getParam(self::$LON);
                $lat = $this->_request->getParam(self::$LAT);

                $model = new Model_Place();
                $rs = $model->api_searchPlaceByLocation($lon, $lat, $page, $count);

                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
            }
        }
    }
    
    public static $URL = "http://download.apk";   
    public static $MESSAGE = "Phiên bản chính thức của SunnyPlace";
    public static $CURRNET_VER = 13;
    public static $VER = "version";
    public function updateAction(){
        $this->_helper->viewRenderer->setNoRender(true);
        $version = $this->_request->getParam(self::$VER);
        if ($version == "") {
            echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
            die;
        }
        if (self::$CURRNET_VER == $version) {
            echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_UPDATES));
        } else {            
            $data = array();
            $data = Sunny_ErrorHandled::handleSuccess();
            $data['data']['version'] = self::$CURRNET_VER;
            $data['data']['download'] = self::$URL;
            $data['data']['message'] = self::$MESSAGE;
            echo json_encode($data);
        }
    }

    public function blockAction() {

        $this->checkPermission($this->_request->getParams());
        $this->_helper->viewRenderer->setNoRender(true);
        $method = $this->_request->getParam(self::$METHOD);

        if ($method != null) {
            if ($method == self::$GET_BLOCK) {
                $id = $this->_request->getParam(self::$ID);
                $page = $this->_request->getParam(self::$PAGE);
                $count = $this->_request->getParam(self::$COUNT);

                if ($count == null || $page == null) {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$INVAILD_PAGE_OR_COUNT));
                    die;
                }

                $model = new Model_Block();
                $rs = $model->api_getBlock($page, $count);


                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
            }
        }
    }

    public function reviewAction() {
        $this->checkPermission($this->_request->getParams());
        $this->_helper->viewRenderer->setNoRender(true);
        $method = $this->_request->getParam(self::$METHOD);

        if ($method != null) {
            if ($method == self::$GET_REVIEW_BY_ID) {
                $id = $this->_request->getParam(self::$ID);

                $model = new Model_Review();
                $rs = $model->api_getReviewById($id);


                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
            }
        }
    }

    public function mapAction() {
        $this->checkPermission($this->_request->getParams());
        $this->_helper->viewRenderer->setNoRender(true);
        $method = $this->_request->getParam(self::$METHOD);

        if ($method != null) {
            if ($method == self::$GET_MAP_BY_ID) {
                $id = $this->_request->getParam(self::$ID);

                $model = new Model_Map();
                $rs = $model->api_getMapById($id);
                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
            }
        }
    }

    public function menuAction() {
        $this->checkPermission($this->_request->getParams());
        $this->_helper->viewRenderer->setNoRender(true);
        $method = $this->_request->getParam(self::$METHOD);

        if ($method != null) {
            if ($method == self::$GET_MENU_BY_ID) {
                $id = $this->_request->getParam(self::$ID);

                $model = new Model_Menu();
                $rs = $model->api_getMenuById($id);
                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
            }
        }
    }

    public function siteAction() {
        $this->checkPermission($this->_request->getParams());
        $this->_helper->viewRenderer->setNoRender(true);
        $method = $this->_request->getParam(self::$METHOD);
        if ($method != null) {
            if ($method == self::$GET_ALL_SITE) {
                $model = new Model_Site();
                $rs = $model->getAllSite();
                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
            }
        }
    }

    public function typeAction() {
        $this->checkPermission($this->_request->getParams());
        $this->_helper->viewRenderer->setNoRender(true);
        $method = $this->_request->getParam(self::$METHOD);

        if ($method != null) {
            if ($method == self::$GET_ALL_TYPE) {
                $model = new Model_Type();
                $rs = $model->getAllType();
                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
            }
        }
    }

    public function placeAction() {
        $this->checkPermission($this->_request->getParams());
        $this->_helper->viewRenderer->setNoRender(true);
        $method = $this->_request->getParam(self::$METHOD);
        if ($method != null) {
            if ($method == self::$GET_PLACE_BY_SITE_ID) {
                $id = $this->_request->getParam(self::$ID);

                $count = $this->_request->getParam(self::$COUNT);
                $page = $this->_request->getParam(self::$PAGE);

                if ($count == null || $page == null) {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$INVAILD_PAGE_OR_COUNT));
                    die;
                }

                $model = new Model_Place();
                $rs = $model->api_getPlaceBySiteId($id, $page, $count);

                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
            }
            if ($method == self::$GET_PLACE_BY_TYPE_ID) {
                $id = $this->_request->getParam(self::$ID);

                $count = $this->_request->getParam(self::$COUNT);
                $page = $this->_request->getParam(self::$PAGE);

                if ($count == null || $page == null) {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$INVAILD_PAGE_OR_COUNT));
                    die;
                }

                $model = new Model_Place();
                $rs = $model->api_getPlaceByTypeId($id, $page, $count);

                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
            }
            if ($method == self::$GET_PLACE_BY_BLOCK_ID) {
                $id = $this->_request->getParam(self::$ID);

                $count = $this->_request->getParam(self::$COUNT);
                $page = $this->_request->getParam(self::$PAGE);
                if ($count == null || $page == null) {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$INVAILD_PAGE_OR_COUNT));
                    die;
                }
                $model = new Model_Place();
                $rs = $model->api_getPlaceByBlockId($id, $page, $count);

                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
            }
            if ($method == self::$GET_PLACE_DETAIL_BY_ID) {
                $id = $this->_request->getParam(self::$ID);

                $model = new Model_Place();
                $rs = $model->api_getPlaceDetailById($id);

                if ($rs != null) {
                    echo $this->returnJSON($rs);
                } else {
                    echo (Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NO_DATA_RECORD));
                }
            }
        }
    }

    private function returnJSON($arrData) {
        if ($arrData != null) {
            $data = array();
            $data = Sunny_ErrorHandled::handleSuccess();
            $data['total'] = count($arrData);
            $data['data'] = $arrData;
            return json_encode($data);
        }
    }

}
?>

